<template>
  <base-container>
    <!-- 搜索栏 -->
    <avue-table-search
      :option="searchOption"
      v-model="searchParams"
      @search-change="loadTableSearch"
      @search-reset="loadTableSearchReset"
    />
    <base-table-card>
      <template #header-right>
        <el-button type="primary" @click="openAddDialog">新增版本</el-button>
      </template>
      <avue-crud
        ref="crudRef"
        :option="tableOption"
        :data="tableData"
        v-model="formData"
        :page="page"
        :table-loading="tableLoading"
        @refresh-change="loadGetTableData"
        @current-change="loadTablePageChange"
        @size-change="loadTablePageSizeChange"
        @row-save="loadAdd"
        @row-update="loadEdit"
      >
        <template #menu="{ row }">
          <el-button type="primary" text @click="openEditDialog(row)">编辑</el-button>
          <el-button type="primary" text @click="loadDel(row)">删除</el-button>
        </template>
      </avue-crud>
    </base-table-card>
  </base-container>
</template>

<script setup>
  import Option from "@/crudOption/channel/version";

  onMounted(() => {
    // 获取表格数据
    loadGetTableData();
  });

  const crudRef = ref(null);
  function openEditDialog(row) {
    crudRef.value.rowEdit(row); // 内置方法，直接弹出编辑窗口
  }
  function openAddDialog() {
    crudRef.value.rowAdd(); // 内置方法，直接弹出新增窗口
  }

  // 增删改查 hook
  let {
    data: tableData,
    loading: tableLoading,
    page: page,
    search: searchParams,
    form: formData,
    get: loadGetTableData,
    add: loadAdd,
    edit: loadEdit,
    del: loadDel,
    pageNumChange: loadTablePageChange,
    pageSizeChange: loadTablePageSizeChange,
    searchReset: loadTableSearchReset,
    searchChange: loadTableSearch,
  } = useCrudHook({
    get: Api.channel.GetVersionList,
    search: {},
    getParamsFormat: params => {
      // 新增时间
      if (params.createTime && params.createTime.length === 2) {
        params.addBeginDate = params.createTime[0] + " 00:00:00";
        params.addEndDate = params.createTime[1] + " 23:59:59";
        delete params.createTime;
      }
      // 编辑时间
      if (params.updateTime && params.updateTime.length === 2) {
        params.updateBeginDate = params.updateTime[0] + " 00:00:00";
        params.updateEndDate = params.updateTime[1] + " 23:59:59";
        delete params.expiryTime;
      }
      // 返回查询参数
      return params;
    },

    add: Api.channel.AddMiniVersion,
    edit: Api.channel.UpdateMiniVersion,
    del: Api.channel.DelMiniVersion,
  });
  // 获取 搜索配置 和 表格配置
  const { searchOption, tableOption } = Option(formData);
</script>
<style lang="scss" scoped></style>
